package com.gilight.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.common.Constant;
import com.gilight.base.dto.ColdCategoryDto;
import com.gilight.base.model.ColdCategory;
import com.gilight.base.service.IColdCategoryService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * @author shr
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/web/coldCategory")
public class ColdCategoryController {

    @Autowired
    private IColdCategoryService coldCategoryServiceImpl;


    @ApiOperation(value = "根据id查询")
    @RequestMapping(value = "/selectById", method = {RequestMethod.GET})
    public ResultEntity selectById(ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(coldCategoryServiceImpl.getById(coldCategory.getId()));
        return resultEntity;
    }

    @ApiOperation(value = "条件查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultEntity list(ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(coldCategoryServiceImpl.getProductList(coldCategory.getZzjgId()));
        return resultEntity;
    }

    @ApiOperation(value = "分页条件查询")
    @RequestMapping(value = "/page", method = {RequestMethod.GET})
    public ResultEntity page(ColdCategory coldCategory,
                            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        ResultEntity resultEntity = new ResultEntity();
        QueryWrapper<ColdCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coldCategory.getZzjgId()!=null,"zzjg_id",coldCategory.getZzjgId())
                    .like(coldCategory.getName()!=null&&!"".equals(coldCategory.getName()),"name",coldCategory.getName())
                    .eq(coldCategory.getStatus()!=null,"status",coldCategory.getStatus())
                    .eq(coldCategory.getIsNew()!=null,"is_new",coldCategory.getIsNew())
                    .orderByAsc("sort_number")
                    .orderByDesc("id");
        Page page = new Page(pageNo,pageSize);
        resultEntity.setData(coldCategoryServiceImpl.page(page,queryWrapper));
        return resultEntity;
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public ResultEntity save(@RequestBody @Validated ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.save(coldCategory);
        return resultEntity;
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/updateById", method = {RequestMethod.POST})
    public ResultEntity updateById(@RequestBody ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.updateById(coldCategory);
        return resultEntity;
    }

    @ApiOperation(value = "添加或更新")
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    public ResultEntity saveOrUpdate(@RequestBody ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.saveOrUpdate(coldCategory);
        return resultEntity;
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/removeById", method = {RequestMethod.POST})
    public ResultEntity removeById(@RequestBody ColdCategory coldCategory) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.removeById(coldCategory.getId());
        return resultEntity;
    }

    @ApiOperation(value = "给商品分类添加商品")
    @RequestMapping(value = "/addProductList", method = {RequestMethod.POST})
    public ResultEntity addProductList(@RequestBody ColdCategoryDto coldCategoryDto) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.addProductList(coldCategoryDto);
        return resultEntity;
    }
    @ApiOperation(value = "从商品分类添移除商品")
    @RequestMapping(value = "/removeProductList", method = {RequestMethod.POST})
    public ResultEntity removeProductList(@RequestBody ColdCategoryDto coldCategoryDto) {
        ResultEntity resultEntity = new ResultEntity();
        coldCategoryServiceImpl.removeProductList(coldCategoryDto);
        return resultEntity;
    }

}
